import Cookies from 'js-cookie';

const TokenKey = 'token';
export const toString = Object.prototype.toString;

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  return Cookies.set(TokenKey, token);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

export function cook(k, v) {
  if (toString.call(v) === '[object Undefined]') {
    const c = Cookies.get(k);
    if (c === 'false') {
      return false;
    }
    if (c === 'true') {
      return true;
    }
    return toString.call(c) === '[object Undefined]' ? false : c;
  } else {
    return Cookies.set(k, v);
  }
}

export function removeCook(k) {
  k = toString.call(k) === '[object String]' ? [k] : k;
  return k.forEach((cur) => {
    Cookies.remove(cur);
  });
}
